﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LDSP.BRL;
using System.Text;
using System.Data;

namespace LDSP.Helper
{
    public class StringUtil
    {
        public static string GetSearchText(int number)
        {
            return "(搜索结果共计" + number + "条)";
        }

        public static string ConstructTitle(Codification ett)
        {
            string category = ett.Category != null ? ett.Category.Name : string.Empty;
            string orgnization = ett.Organization;
            string referenceLabel = ett.DocType == DocType.法律法规 ? "发布文号" : "标准号";
            string referenceNumber = ett.ReferenceNumber;
            string publishDate = ett.ReleaseDT.ToShortDateString();
            string implementDate = ett.ImplementDT.ToShortDateString();
            string isValid = ett.IsValid == true ? "是" : "否";

            return "类别：" + category
                + "\n发布机构：" + orgnization
                + "\n" + referenceLabel + "：" + referenceNumber
                + "\n发布日期：" + publishDate
                + "\n生效日期：" + implementDate
                + "\n是否有效：" + isValid;
        }

        public static string ConstructTitle(AccidentCase ett)
        {
            string type = ett.AccidentCaseType != null ? ett.AccidentCaseType.Name : string.Empty;

            return "类别：" + type;
        }

        public static string ConstructChemicalTitle(Chemical ett)
        {
            StringBuilder sb = new StringBuilder(100);
            sb.Append("【别名】：").AppendLine(ett.Alias)
                .Append("【危险货物编号】：").AppendLine(ett.DangerNumber)
                .Append("【主要燃爆危险性分类】：").AppendLine(ett.DangerType)
                .Append("【海关商品编号】：").AppendLine(ett.GoodsNumber)
                .Append("【计量单位】：").AppendLine(ett.Unit)
                .Append("【包装类别】：").AppendLine(ett.PackageType)
                .Append("【受限范围】：").AppendLine(ett.LimitedScope)
                .Append("【运输方式】：").AppendLine(ett.TransportMethod)
                .Append("【英文名称】：").AppendLine(ett.EnglishName)
                .Append("【类别或项目】：").AppendLine(ett.TypeCatalog)
                .Append("【次要危险性】：").AppendLine(ett.SecondaryDangerous)
                .Append("【例外数量】：").AppendLine(ett.ExceptionNumber)
                .Append("【限制数量】：").AppendLine(ett.LimitationNumber);
            return sb.ToString();
        }

        public static string TaskPlanTitle(Plan ett)
        {
            StringBuilder sb = new StringBuilder(100);
            sb.Append("【法律法规/标准名称】：").AppendLine(ett.EnterpriseCodification.Codification.Name)
                .Append("【执行部门/人员】：").AppendLine(ett.Department)
                .Append("【备注】：").AppendLine(ett.Comment);
            return sb.ToString();
        }

        public static string ConvertToHtml(DataTable dt, params string[] headers)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("详细信息:<br/>");
            sb.Append("<table style=\"border: 0px solid #000000; border-collapse: collapse;\">");

            if (dt != null)
            {
                IList<string> headerlist = new List<string>();
                foreach (string header in headers)
                {
                    headerlist.Add(header);
                }

                //生成表头
                sb.Append(GenerateHtmlBoldRow(headerlist));

                foreach (DataRow row in dt.Rows)
                {
                    IList<string> celllist = new List<string>();
                    foreach (object cell in row.ItemArray)
                    {
                        celllist.Add(cell.ToString());
                    }

                    //生成行
                    sb.Append(GenerateHtmlRow(celllist));
                }
            }

            sb.Append("</table>");

            return sb.ToString();
        }

        public static string GenerateHtmlRow(IList<string> list)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr>");
            foreach (string s in list)
            {
                sb.Append(string.Format("<td style=\"border-style: solid; border-width: thin\">{0}</td>", s));
            }
            sb.Append("</tr>");

            return sb.ToString();
        }

        public static string GenerateHtmlBoldRow(IList<string> list)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr>");
            foreach (string s in list)
            {
                sb.Append(string.Format("<td style=\"border-style: solid; border-width: thin;font-weight: bold;\">{0}</td>", s));
            }
            sb.Append("</tr>");

            return sb.ToString();
        }
    }
}